package com.atguigu.lease.web.admin.controller.login;


import com.atguigu.lease.common.jwts.JwtUtil;
import com.atguigu.lease.common.login.LoginUserHolder;
import com.atguigu.lease.common.result.Result;

import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private LoginService loginService;


    @Autowired
    private SystemUserService systemUserService;

    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        CaptchaVo captchaVo = loginService.customCaptcha();
        return Result.ok(captchaVo);
    }

    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        String token = loginService.customLogin(loginVo);
        return Result.ok(token);
    }

//    @Operation(summary = "获取登陆用户个人信息")
//    @GetMapping("info")
//    public Result<SystemUserInfoVo> info(@RequestHeader("access_token") String accessToken) {
//        //1.解析头获取userId
//        //会报类型转化异常
//        //存储：jwt -> Long -> userId -> int
//        //不能使用Long
//        Integer userId =Integer.parseInt(JwtUtil.parseToken(accessToken).get("userId")+"");
//        SystemUser systemUser = systemUserService.getById(userId);
//        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
//        BeanUtils.copyProperties(systemUser,systemUserInfoVo);
//        return Result.ok(systemUserInfoVo);
//    }
    @Operation(summary = "获取登录用户个人信息")
    @GetMapping("info")
    public Result<SystemUserInfoVo> info() {
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        SystemUserInfoVo systemUserInfoVo = loginService.getLoginUserInfoById(userId);
        return Result.ok(systemUserInfoVo);
    }
}